<template>
  <section class="summary-list" v-loading="loading">
    <div class="item" v-for="(item, index) in list" :key="index" @click="$emit('change-month', item.month)">
      <div class="subtitle">{{ item.month }}月</div>
      <div class="tr th">
        <span class="empty" />
        <span>集团</span>
        <span>集群</span>
        <span>事业部</span>
      </div>
      <div class="tr">
        <span>检查项</span>
        <span>{{ item.dept1CheckCounts }}</span>
        <span>{{ item.dept2CheckCounts }}</span>
        <span>{{ item.dept3CheckCounts }}</span>
      </div>
      <div class="tr">
        <span>单月合格率</span>
        <span :class="item.dept1PassPercent | scoreClass">{{ item.dept1PassPercent }}%</span>
        <span :class="item.dept2PassPercent | scoreClass">{{ item.dept2PassPercent }}%</span>
        <span :class="item.dept3PassPercent | scoreClass">{{ item.dept3PassPercent }}%</span>
      </div>
      <div class="tr">
        <span>累计合格率</span>
        <span :class="item.dept1AllPercent | scoreClass">{{ item.dept1AllPercent }}%</span>
        <span :class="item.dept2AllPercent | scoreClass">{{ item.dept2AllPercent }}%</span>
        <span :class="item.dept3AllPercent | scoreClass">{{ item.dept3AllPercent }}%</span>
      </div>
      <div class="tr tr2">
        <span>事业部专项检查得分</span>
        <span :class="item.specialCheckScore | scoreClass">{{ item.specialCheckScore || '/' }}</span>
      </div>
      <div class="tr tr2">
        <span>工程得分</span>
        <span :class="item.projectScore | scoreClass">{{ item.projectScore || '/' }}</span>
      </div>
      <div class="tr tr2" v-if="item.showAccidentTitle" @click.stop="openRisks(item)">
        <span>重大风险</span>
        <span class="red ellipsis accident-title" :title="item.showAccidentTitle">{{ item.showAccidentTitle }}</span>
      </div>
      <div class="tr tr2" v-else><span>重大风险</span><span>/</span></div>
    </div>

    <el-dialog :title="risk.title" :visible.sync="risk.show" width="800px" append-to-body>
      <el-collapse v-model="risk.active">
        <el-collapse-item :name="index" v-for="(item, index) in risk.list" :key="index">
          <div slot="title" class="ellipsis" :title="item.accidentTitle">
            <el-tag size="mini" :type="level2color[item.level]" style="margin-right: 5px">{{ item.level | selectEnumLabel(proAccidentLevels) }}</el-tag>
            {{ item.accidentTitle }}
          </div>
          <div style="white-space: pre-wrap">{{ item.accidentContent }}</div>
        </el-collapse-item>
      </el-collapse>
    </el-dialog>
  </section>
</template>
<script>
// 项目推进情况-月度汇总表
import { proAccidentLevels } from '@/utils/baseData'
import { getCheckReportProjectMothInfo } from '@/api/qualityManage/cq'
const level2color = proAccidentLevels.reduce((o, i) => (o[i.value] = i.raw.listClass) && o, {})
export default {
  name: 'MonthlySummary',
  filters: {
    scoreClass(val) {
      return isNaN(val) ? '' : val >= 90 ? 'green' : val >= 80 ? 'blue' : 'red'
    }
  },
  props: {
    queryParams: { type: Object, default: () => ({ projectId: '', inputYear: new Date().getFullYear() + '' }) },
    active: { type: [Number, String], default: '' }
  },
  data() {
    return {
      proAccidentLevels,
      level2color,
      loading: false,
      list: [],
      risk: { show: false, title: '', list: [], active: [] }
    }
  },
  methods: {
    reset() {
      this.loading = false
      this.list = []
    },
    async getList() {
      this.loading = true
      const res = await getCheckReportProjectMothInfo(this.queryParams).catch(() => false)
      this.loading = false
      if (res === false) return (this.list = [])
      const data = res.data || []

      data.map(i => {
        // 去除年份，和月份前缀0
        i.month = +i.checkMonth.substring(4)
        if (i.accidents) i.showAccidentTitle = i.accidents.map(r => r.accidentTitle).join(',')
      })
      this.list = data
    },
    openRisks(item) {
      this.risk = {
        show: true,
        title: `${item.month}月项目事故`,
        list: item.accidents,
        active: [0]
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.summary-list {
  min-height: 300px;
  position: relative;
  left: 0;
  transition: all 0.4s ease;
  display: flex;
  flex-wrap: wrap;
  .item {
    position: relative;
    width: 350px;
    background-color: white;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 2px 12px 0 rgba(#000, 0.01);
    font-size: 12px;
    color: #606266;
    margin: 0 20px 20px 0;
    cursor: pointer;
    transition: 0.3s;
    &:hover {
      transform: translate(-1px, -1px);
      box-shadow: 0 2px 12px rgba(#000, 0.1);
    }
  }
  .subtitle {
    position: absolute;
    left: 0;
    top: 0;
    background-image: linear-gradient(to right, #2ce6c9, #00baff);
    color: white;
    font-weight: 700;
    padding: 5px 15px;
    border-radius: 9px 0 10px 0;
  }
  .tr {
    display: flex;
    padding: 5px 0;
    span {
      &:first-child {
        width: 40%;
        color: #373839;
        &:not(.empty)::before {
          content: '●';
          color: #cddafc;
          margin-right: 3px;
        }
      }
      &:not(:first-child) {
        width: 20%;
        text-align: center;
      }
    }
  }
  .tr2 {
    background-color: #f8fafe;
    padding: 5px;
    margin-top: 5px;
    border-radius: 5px;
    span {
      &:not(:first-child) {
        width: 60%;
      }
    }
  }
  .th {
    color: #515a6e;
    font-weight: bold;
    pointer-events: none;
  }
  .green {
    color: #4eab59;
  }
  .blue {
    color: #409eff;
  }
  .red {
    color: #e56662;
  }
  .accident-title:hover {
    text-decoration: underline;
  }
}
</style>
